var form;
var layer;
var layedit;
var table;
var resType = null;
layui.use(['form', 'layedit', 'laydate', 'table'], function () {
    form = layui.form,
        layer = layui.layer,
        layedit = layui.layedit,
        laydate = layui.laydate;
    table = layui.table;
    laydate.render({
        elem: '#res-date',
        range: true
    });
    var $tree = $('#tree').jstree({
        "core": {
            "animation": 0,
            "check_callback": true,
            "themes": {"stripes": true},
            'data': function (obj, callback) {
                var url = '/doctype/list';
                if (obj.id !== '#') {
                    url += "?id=" + obj.id
                }
                var nodes = [];
                $.getJSON(url, function (res) {
                    if (res.code === 200) {
                        res.data.forEach(function (i) {
                            nodes.push({
                                id: i.id,
                                parent: i.parentId == 0 ? "#" : i.parentId,
                                text: i.name,
                                children: i.hasChild === 0 ? false : true,
                                data: i
                            });
                        });
                        callback.call(this, nodes);
                    }
                });
            }
        },
        "plugins": [
            "dnd", "search", "state", "types", "wholerow"
        ]
    }).on('loaded.jstree', function (e, data) {
        var inst = data.instance;
        var obj = inst.get_node(e.target.firstChild);
        inst.select_node(obj, false)
    });
    $('#res-add-btn').click(function () {
        window.location.href = 'data-add?resType=' + resType;
    });
    $('#create-btn').click(function () {
        var url = '/doctype-add';
        $ref = $.jstree.reference('#tree');
        if ($ref._model.data['#'].children.length == 0) {
            url += '?parentId=0&id=0'
        } else {
            var sel = $ref.get_selected();
            if (!sel.length) {
                alert("请先选择一个节点");
                return;
            }
            sel = sel[0];
            var parent = 0;
            if ($ref.get_node(sel).parent !== '#') {
                parent = $ref.get_node(sel).parent;
            }
            url += '?parentId=' + parent + '&id=' + sel;
        }
        layer.open({
            type: 2,
            title: '添加分类',
            content: url,
            area: ['600px', '400px'],
            end: function (index, layero) {
                table.reload('apiTable');
                $ref.refresh();
            }
        });
    });

    $('#del-btn').click(function () {
        $ref = $.jstree.reference('#tree');
        var sel = '#';
        if ($ref._model.data['#'].children.length == 0) {
            layer.msg('当前列表为空')
        } else {
            sel = $ref.get_selected();
            if (!sel.length) {
                layer.msg("请先选择一个节点");
                return;
            }
            sel = sel[0];
            if ($ref.get_node(sel).data.hasChild === 1) {
                layer.msg("该节点包含子节点，不可删除");
                return;
            }
        }
        var index = layer.open({
            type: 1,
            content: '<div style="padding: 40px;">删除后不可恢复，确认要删除吗？🤔️🤔️</div>️',
            btn: ['确认', '取消'],
            btn1: function (index, layero) {
                $.getJSON('/doctype/del?id=' + sel, function (res) {
                    if (res.code == 200) {
                        $ref.refresh();
                        layer.close(index);
                    } else {
                        layer.msg('删除失败');
                    }
                })

            },
            btn2: function (index, layero) {
                //按钮【按钮三】的回调
                $ref.refresh();
            }
        });
    });

    $tree.on('select_node.jstree', function (e, data) {
        resType = data.node.data.id;
        table.render({
            elem: '#res-tables',
            url: '/doc/list?typeId=' + resType,
            cellMinWidth: 80,
            cols: [[
                {field: 'id', width: 80, title: 'ID', sort: true},
                {field: 'name', title: '名称'},
                {field: 'department', width: 160, title: '单位', sort: true},
                {field: 'area',  title: '区域'},
                {field: 'createTime', width: 140, title: '创建时间', templet: function (d) {
                    return d.createTime.split('T')[0];
                    }},
                {field: 'secret', width: 180, title: '密级', templet: function (d) {
                   if (d.secret == 1) {
                        return "秘密";
                   } else if(d.secret == 2){
                        return "机密";
                   }else {
                       return "非密";
                   }
                }},
                {fixed: 'right', title: '操作', toolbar: '#tool-bar', width: 180}
            ]]
            , page: true
        });
        table.on('tool(res-tables)', function(obj){
            var data = obj.data;
            console.log(data);
            if(obj.event === 'preview'){
                layer.open({
                    type: 2,
                    width: '600px',
                    content:'/file-list?docId=' + data.id //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                });
            }
        });
    });


});


